package model

import (
	"fmt"
	"ginStudy/utils"
	_ "github.com/go-sql-driver/mysql"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

/**
* @Author: 俊杰
* @Description: db入口文件，配置数据库参数
* @File: db
* @Version: 1.0.0
* @Date: 2022/5/25 22:31
 */

var (
	db *gorm.DB
	err error
)

func InitDb()  {
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		utils.DbUser,
		utils.DbPassWord,
		utils.DbHost,
		utils.DbPort,
		utils.DbName,
	)

	db,err = gorm.Open(mysql.Open(dsn),&gorm.Config{})

	if err != nil {
		fmt.Println("数据库连接失败！  ",err)
	}
	db.AutoMigrate(&User{},&Article{},&Category{}) //自动迁移，就是在数据库中建库


}

